// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    load: true,
    list: [],
    obj: {},
    page: 10,
    vids: [],
    movieIds: []
  },
  //请求更多数据
  loadData: function(url){
    this.setData({load: true});
    wx.request({
      url: url,
      header: { "content-type": "json"},
      success: (res) => {
         let data = res.data.coming;
         let obj = this.data.obj,list = [];
         data.forEach( item => {
             item.img = item.img.replace('w.h','165.180');
             var tem = obj[item.comingTitle] || [];
             tem.push(item);
             obj[item.comingTitle] = tem;
         });
         for(var attr in obj){
           var tem = {};
           tem['time'] = attr; 
           tem['data'] = obj[attr];
           list.push(tem);
         };

         if(!this.data.movieIds.length){ 
           this.setData({movieIds:res.data.movieIds}); 
           var vids = [];
          }else {
            var vids = this.data.movieIds.splice(this.data.page,10);
            if(vids.length ==0) {
              wx.showToast({
                title: '已到达底部',
              });
              this.setData({load: false});
              return false;
            }
         }
         this.setData({
             list,
             vids,
             page: this.data.page+10,
             load: false
         });
      }
    })
  },
  onLoad: function () {
     this.loadData('http://m.maoyan.com/ajax/comingList?ci=70&token=&limit=10');
  },
  onReachBottom: function () {
    this.loadData('http://m.maoyan.com/ajax/moreComingList?ci=70&token=&limit=10&movieIds='+ this.data.vids.join(','));
  }
})